import traceback
import os
import sys
import time

import facade
from xjlibrary.mrequest.baserequest import BaseRequest
from xjlibrary.our_file_dir.base_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curPath, "db.ini")
logger = facade.get_streamlogger()
mysqlutils = facade.MysqlUtiles(configfile, "db", logger=logger)


sGUrl = r'http://192.168.52.10:8080/adksvod/PublicFolder/Player/VideoPlay.aspx?VideoID='
HEADERS = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive',
           'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR '
                         '2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)', }
sSqlList = list()
nCount = 0
StartTime = time.time()

Proxies = {
    'http': '192.168.30.176:8120',
    'https': '192.168.30.176:8120'
}


# 测试文字输出到文本
def output(value, files='log.txt'):
    BaseDir.single_write_file(value, files)


# 获取详情页列表
def GetHtml(nNum):
    sSql = "SELECT vid, `catalog`, timespan, pubdate FROM article WHERE stat = 0 AND failcount < 3  LIMIT {nNum}".format(nNum=nNum)
    rows = mysqlutils.SelectFromDB(sSql)
    return rows


# 下载详情页面
def DownPage(taskList):
    global sSqlList, nCount
    vid = None
    for task in taskList:
        if not task:
            print('* Task Error')
            sys.exit(-1)
        vid = task[0]
        catalog = task[1]
        timespan = task[2]
        pubdate = task[3]
        pageUrl = sGUrl + vid
        print('开始下载:', pageUrl)
        bFlag, errString, r = RequestDetailPage(pageUrl)

        # 判断是否请求详情页成功 bFlag（1：成功，0：失败）
        if not bFlag:
            sSql = "UPDATE article set failcount = failcount + 1 where vid = '{vid}'".format(vid=vid)
        else:
            html = r.content.decode('utf-8').strip()  # 去除首尾空格
            line = vid + '\t' + catalog + '\t' + timespan + '\t' + pubdate + '★' + html.replace('\0', ' ').replace('\r',
                                                                                                                   ' ').replace(
                '\n', ' ')
            dirPath = os.path.join(curPath, 'Detail', catalog)
            if not os.path.exists(dirPath):
                os.makedirs(dirPath)

            outPathFile = os.path.join(dirPath, vid + '.htm')
            # 将内容写入文件
            BaseDir.single_write_file(line, outPathFile)
            nCount += 1
            print('写入文件数量：%d' % nCount)
            print('耗费时间: %d' % int(time.time() - StartTime) + '秒')
            sSql = "UPDATE article set stat = 1 where vid = '{vid}'".format(vid=vid)
            print('DOWN:' + str(nCount))

        sSqlList.append(sSql)
        if len(sSqlList) > 100:
            UpdateDbById()

    UpdateDbById()


# 请求详情页
def RequestDetailPage(pageUrl):
    BoolResult, errString, r = BaseRequest(pageUrl,
                                           headers=HEADERS,
                                           proxies=Proxies,
                                           mark='<div class="topic">',
                                           timeout=30)
    return BoolResult, errString, r


# 更新数据库状态
def UpdateDbById():
    global sSqlList
    mysqlutils.ExeSqlListToDB(sSqlList)
    sSqlList = list()


# ----------------------------------------------逻辑部分-----------------------------------------------
# ----------------------------------------------下载详情页---------------------------------------------

if __name__ == '__main__':

    # 开始作业
    while True:
        taskList = GetHtml(1000)
        if not taskList:
            break
        DownPage(taskList)

# print('DOWN TOTAL:' + str(nCount))
